package asdf.springmvc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lichengwen
 * @since 2016/4/1 18:06
 */
@ControllerAdvice
public class ControllerDeamon {

    private static final Logger LGR = LoggerFactory.getLogger(ControllerDeamon.class);

    @ExceptionHandler
    public ModelAndView handleException(Throwable t) {
        LGR.error("handle exception", t);
        return new ModelAndView();
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        binder.registerCustomEditor(String.class,
                new StringTrimmerEditor(true));
    }

}
